﻿using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.SuperGrid;
using DevComponents.DotNetBar.SuperGrid.Style;
using Kimd.Common;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI.Controls
{
    public partial class ModuleControl : UserControl
    {
        private List<string> _cardConfig;
        public string ModuleName { get; set; }
        public List<int> ContainerSplitDistance { get; set; }
        public ModuleControl(List<string> cardConfig)
        {
            InitializeComponent();
            ContainerSplitDistance = new List<int>();
            _cardConfig = cardConfig;
        }
        private void ModuleControl_Resize(object sender, EventArgs e)
        {
            LoadLayout();
        }
        private void resizePanel()
        {
            
        }
        public void AddDigitalInput(string name, bool status, string source, string id, bool alwaysOn, Sensor sensor)
        {
            GridRow gridRow = new GridRow();

            gridRow.Tag = sensor;
            sensor.CardConfig = _cardConfig;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = name;
            GridCell gridCell3 = new GridCell();
            gridCell3.Value = source;
            GridCell gridCell4 = new GridCell();
            gridCell4.Value = id;
            GridCell gridCell5 = new GridCell();
            gridCell5.Value = alwaysOn;

            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell3);
            gridRow.Cells.Add(gridCell4);
            gridRow.Cells.Add(gridCell5);


            this.sGC_Input.PrimaryGrid.Rows.Add(gridRow);

            GridRow gridRow2 = new GridRow();

            gridRow2.Tag = sensor;

            GridCell gridCell2 = new GridCell();
            gridCell2.Value = status ? "圆形选中32.png" : "圆形未选中32.png";
            gridCell2.ReadOnly = true;
            gridRow2.Cells.Add(gridCell2);
            this.sGC_InputStatus.PrimaryGrid.Rows.Add(gridRow2);
        }
        public void AddDigitalOutput(string name, bool status, bool onOff, string source, string id, Cylinder cylinder, string source2 = "", string id2 = "", string origin = "", string move = "")
        {
            GridRow gridRow = new GridRow();

            gridRow.Tag = cylinder;
            cylinder.CardConfig = _cardConfig;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = name;
            GridCell gridCell4 = new GridCell();
            gridCell4.Value = source;
            GridCell gridCell5 = new GridCell();
            gridCell5.Value = id;
            GridCell gridCell6 = new GridCell();
            gridCell6.Value = source2;
            GridCell gridCell7 = new GridCell();
            gridCell7.Value = id2;
            GridCell gridCell8 = new GridCell();
            gridCell8.Value = origin;
            GridCell gridCell9 = new GridCell();
            gridCell9.Value = move;

            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell4);
            gridRow.Cells.Add(gridCell5);
            gridRow.Cells.Add(gridCell6);
            gridRow.Cells.Add(gridCell7);
            gridRow.Cells.Add(gridCell8);
            gridRow.Cells.Add(gridCell9);

            this.sGC_Output.PrimaryGrid.Rows.Add(gridRow);

            GridRow gridRow2 = new GridRow();
            gridRow2.Tag = cylinder;

            GridCell gridCell2 = new GridCell();
            gridCell2.Value = status ? "圆形选中32.png" : "圆形未选中32.png";
            GridCell gridCell3 = new GridCell();
            gridCell3.Value = onOff;
            gridRow2.Cells.Add(gridCell2);
            gridRow2.Cells.Add(gridCell3);

            //if (!string.IsNullOrEmpty(origin))
            {
                GridCell gridCell20 = new GridCell();
                gridCell20.Value = "圆形未选中32.png";
                gridRow2.Cells.Add(gridCell20);
            }

            //if (!string.IsNullOrEmpty(move))
            {
                GridCell gridCell30 = new GridCell();
                gridCell30.Value = "圆形未选中32.png";
                gridRow2.Cells.Add(gridCell30);
            }

            this.sGC_OutputStatus.PrimaryGrid.Rows.Add(gridRow2);
        }
        public void AddAxis(string name, double position, string status, string source, string id, bool hasEncoder, int pulse2MM, int homeMode, bool homeDir, double vel, double start, double end, double acc, double dec, double stime, Axis axis)
        {
            GridRow gridRow = new GridRow();

            gridRow.Tag = axis;
            axis.CardConfig = _cardConfig;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = name;
            
            GridCell gridCell4 = new GridCell();
            gridCell4.Value = source;
            GridCell gridCell5 = new GridCell();
            gridCell5.Value = id;
            GridCell gridCell6 = new GridCell();
            gridCell6.Value = hasEncoder;
            GridCell gridCell7 = new GridCell();
            gridCell7.Value = pulse2MM;
            GridCell gridCell8 = new GridCell();
            gridCell8.Value = homeMode;
            GridCell gridCell9 = new GridCell();
            gridCell9.Value = homeDir;
            GridCell gridCell10 = new GridCell();
            gridCell10.Value = vel;
            GridCell gridCell11 = new GridCell();
            gridCell11.Value = start.ToString("0.0");
            GridCell gridCell12 = new GridCell();
            gridCell12.Value = end.ToString("0.0");
            GridCell gridCell13 = new GridCell();
            gridCell13.Value = acc.ToString("0.00");
            GridCell gridCell14 = new GridCell();
            gridCell14.Value = dec.ToString("0.00");
            GridCell gridCellSTime = new GridCell();
            gridCellSTime.Value = stime.ToString("0.00");
            GridCell gridCell15 = new GridCell();
            gridCell15.Value = "Auto Vel";

            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell4);
            gridRow.Cells.Add(gridCell5);
            gridRow.Cells.Add(gridCell6);
            gridRow.Cells.Add(gridCell7);
            gridRow.Cells.Add(gridCell8);
            gridRow.Cells.Add(gridCell9);
            gridRow.Cells.Add(gridCell10);
            gridRow.Cells.Add(gridCell11);
            gridRow.Cells.Add(gridCell12);
            gridRow.Cells.Add(gridCell13);
            gridRow.Cells.Add(gridCell14);
            gridRow.Cells.Add(gridCellSTime);
            gridRow.Cells.Add(gridCell15);

            this.sGC_Axis.PrimaryGrid.Rows.Add(gridRow);

            GridRow gridRow1 = new GridRow();
            gridRow1.Tag = axis;

            GridCell gridCell100 = new GridCell();
            gridCell100.Value = "OFF";
            gridCell100.ReadOnly = true;
            GridCell gridCell2 = new GridCell();
            gridCell2.Value = position.ToString("0.000");
            gridCell2.ReadOnly = true;
            GridCell gridCell3 = new GridCell();
            gridCell3.Value = $"{status}.png";
            GridCell gridCell200 = new GridCell();
            gridCell200.Value = "NotHome";
            gridCell200.ReadOnly = true;
            GridCell gridCell300 = new GridCell();
            gridCell300.Value = "Stop";
            gridCell300.ReadOnly = true;
            GridCell gridCell400 = new GridCell();
            gridCell400.Value = $"左1.png";
            GridCell gridCell500 = new GridCell();
            gridCell500.Value = $"右1.png";


            GridCell gridCell900 = new GridCell();
            gridCell900.Value = "Reset";
            gridCell900.ReadOnly = true;

            gridRow1.Cells.Add(gridCell100);
            gridRow1.Cells.Add(gridCell2);
            gridRow1.Cells.Add(gridCell3);
            gridRow1.Cells.Add(gridCell200);
            gridRow1.Cells.Add(gridCell300);
            gridRow1.Cells.Add(gridCell400);
            gridRow1.Cells.Add(gridCell500);
            gridRow1.Cells.Add(gridCell900);

            this.sGC_AxisStatus.PrimaryGrid.Rows.Add(gridRow1);
        }
        private void ModuleControl_Load(object sender, EventArgs e)
        {
            List<string> outIO = new List<string>();
            List<string> inIO = new List<string>();
            foreach (string item in _cardConfig)
            {
                if (item.Split('_')[1].Equals("DO8"))
                {
                    for (int i = 0; i < 8; i++)
                    {
                        outIO.Add($"{item}_OUT{i}");
                    }
                }
                else if (item.Split('_')[1].Equals("DIO8"))
                {
                    for (int i = 0; i < 8; i++)
                    {
                        outIO.Add($"{item}_OUT{i}");
                        inIO.Add($"{item}_IN{i}");
                    }
                }
                else if (item.Split('_')[1].Equals("DO16"))
                {
                    for (int i = 0; i < 16; i++)
                    {
                        outIO.Add($"{item}_OUT{i}");
                    }
                }
                else if (item.Split('_')[1].Equals("DIO16"))
                {
                    for (int i = 0; i < 16; i++)
                    {
                        outIO.Add($"{item}_OUT{i}");
                        inIO.Add($"{item}_IN{i}");
                    }
                }
                else if (item.Split('_')[1].Equals("DI8"))
                {
                    for (int i = 0; i < 8; i++)
                    {
                        inIO.Add($"{item}_IN{i}");
                    }
                }
                else if (item.Split('_')[1].Equals("DI16"))
                {
                    for (int i = 0; i < 16; i++)
                    {
                        inIO.Add($"{item}_IN{i}");
                    }
                }
            }
            orderArrayOut = outIO.ToArray();
            orderArrayIn = inIO.ToArray();
            InitDigitalIOGrid();
            InitAxisGrid();

            foreach (var item in SingletonProvider<MachineResource>.Instance.MachineSensor)
            {
                if(item.Value.Module.Equals(ModuleName))
                    AddDigitalInput(item.Key, false, item.Value.ControlCardName, item.Value.ID, item.Value.AlwaysOn, item.Value);
            }
            foreach (var item in SingletonProvider<MachineResource>.Instance.MachineCylinder)
            {
                if (item.Value.Module.Equals(ModuleName))
                    AddDigitalOutput(item.Key, item.Value.AlreadyOn(), item.Value.AlreadyOn(), item.Value.ControlCardName1, item.Value.ID1, item.Value, item.Value.ControlCardName2, item.Value.ID2, string.Join(",",item.Value.OriginNames), string.Join(",", item.Value.MoveNames));
            }
            foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis)
            {
                if (item.Value.Module.Equals(ModuleName))
                    AddAxis(item.Key, 0, "ALM", item.Value.ControlCardName, item.Value.ID, !item.Value.IsStep, (int)item.Value.ConvertRate, item.Value.HomeMode, item.Value.HomeDirP, item.Value.SettingVel[0], item.Value.SettingVelStart[0], item.Value.SettingVelEnd[0], item.Value.SettingAcc[0], item.Value.SettingDec[0], item.Value.SettingSTime[0], item.Value);
            }

            resizePanel();
        }
        string[] orderArrayIn;
        string[] orderArrayOut;
        private void InitDigitalIOGrid()
        {
            this.sGC_InputStatus.PrimaryGrid.Columns["Status"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_InputStatus.PrimaryGrid.Columns["Status"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };

            string[] orderArray = MachineResource.Instance.MachineCard.Keys.ToArray();
            this.sGC_Input.PrimaryGrid.Columns["Source"].EditorType = typeof(FragrantComboBox);
            this.sGC_Input.PrimaryGrid.Columns["Source"].EditorParams = new object[] { orderArray };

            
            this.sGC_Input.PrimaryGrid.Columns["ID"].EditorType = typeof(FragrantComboBox);
            this.sGC_Input.PrimaryGrid.Columns["ID"].EditorParams = new object[] { orderArrayIn };


            this.sGC_OutputStatus.PrimaryGrid.Columns["Status"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_OutputStatus.PrimaryGrid.Columns["Status"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };

            this.sGC_OutputStatus.PrimaryGrid.Columns["Origin"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_OutputStatus.PrimaryGrid.Columns["Origin"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };

            this.sGC_OutputStatus.PrimaryGrid.Columns["Move"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_OutputStatus.PrimaryGrid.Columns["Move"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };

            this.sGC_Output.PrimaryGrid.Columns["Source"].EditorType = typeof(FragrantComboBox);
            this.sGC_Output.PrimaryGrid.Columns["Source"].EditorParams = new object[] { orderArray };
            this.sGC_Output.PrimaryGrid.Columns["Source2"].EditorType = typeof(FragrantComboBox);
            this.sGC_Output.PrimaryGrid.Columns["Source2"].EditorParams = new object[] { orderArray };

            this.sGC_Output.PrimaryGrid.Columns["ID"].EditorType = typeof(FragrantComboBox);
            this.sGC_Output.PrimaryGrid.Columns["ID"].EditorParams = new object[] { orderArrayOut };
            this.sGC_Output.PrimaryGrid.Columns["ID2"].EditorType = typeof(FragrantComboBox);
            this.sGC_Output.PrimaryGrid.Columns["ID2"].EditorParams = new object[] { orderArrayOut };

            this.sGC_Input.ItemDrag += SGC_Input_ItemDrag;
        }
        public void Clear()
        {
            sGC_Input.PrimaryGrid.Rows.Clear();
            sGC_InputStatus.PrimaryGrid.Rows.Clear();
            sGC_Output.PrimaryGrid.Rows.Clear();
            sGC_OutputStatus.PrimaryGrid.Rows.Clear();
            sGC_Axis.PrimaryGrid.Rows.Clear();
            sGC_AxisStatus.PrimaryGrid.Rows.Clear();
        }
        public void ClearSensor()
        {
            sGC_Input.PrimaryGrid.Rows.Clear();
            sGC_InputStatus.PrimaryGrid.Rows.Clear();
        }
        public void ClearCylinder()
        {
            sGC_Output.PrimaryGrid.Rows.Clear();
            sGC_OutputStatus.PrimaryGrid.Rows.Clear();
        }
        public void ClearAxis()
        {
            sGC_Axis.PrimaryGrid.Rows.Clear();
            sGC_AxisStatus.PrimaryGrid.Rows.Clear();
        }
        SuperGridControl _dragSourceGrid;
        GridElement _dragSourceElement;
        GridRow _dragOverRow;
        GridCell _dragOverCell;
        GridColumn _dragOverColumn;
        private void SGC_Input_ItemDrag(object sender, GridItemDragEventArgs e)
        {
            SuperGridControl sg = sender as SuperGridControl;

            if (sg != null)
            {
                // Get the collection of selected items

                SelectedElementCollection items = null;

                if (e.Item is GridCell)
                    items = sg.PrimaryGrid.GetSelectedCells();

                else if (e.Item is GridRow)
                    items = sg.PrimaryGrid.GetSelectedRows();

                else if (e.Item is GridColumn)
                    items = sg.PrimaryGrid.GetSelectedColumns();

                // If the user has selected items, then make the
                // data accessible, and start the drag operation.

                if (items != null && items.Count > 0)
                {
                    _dragSourceGrid = sg;
                    _dragSourceElement = e.Item as GridElement;

                    DataObject dob = new DataObject(items);

                    if (e.Item is GridRow)
                    {
                        Node[] nodes = new Node[items.Count];

                        for (int i = 0; i < items.Count; i++)
                        {
                            GridRow row = (GridRow)items[i];

                            Node node = new Node();
                            node.Cells.Clear();

                            foreach (GridCell cell in row.Cells)
                            {
                                DevComponents.AdvTree.Cell acell = new DevComponents.AdvTree.Cell(cell.Value.ToString());

                                node.Cells.Add(acell);
                            }

                            nodes[i] = node;
                        }

                        dob.SetData(nodes);
                    }

                    // Initiate the drag operation

                    sg.DoDragDrop(dob, DragDropEffects.Copy | DragDropEffects.Move);

                    _dragSourceGrid = null;
                    _dragSourceElement = null;
                }

                //ClearDragHighlight();
            }
        }

        private void InitAxisGrid()
        {
            string[] orderArray = MachineResource.Instance.MachineCard.Keys.ToArray();
            this.sGC_Axis.PrimaryGrid.Columns["Source"].EditorType = typeof(FragrantComboBox);
            this.sGC_Axis.PrimaryGrid.Columns["Source"].EditorParams = new object[] { orderArray };

            string[] orderArrayAxis = { "Axis0", "Axis1", "Axis2", "Axis3", "Axis4", "Axis5", "Axis6", "Axis7", };
            this.sGC_Axis.PrimaryGrid.Columns["ID"].EditorType = typeof(FragrantComboBox);
            this.sGC_Axis.PrimaryGrid.Columns["ID"].EditorParams = new object[] { orderArrayAxis };

            string[] homeModeArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
                                       "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",};
            this.sGC_Axis.PrimaryGrid.Columns["HomeMode"].EditorType = typeof(FragrantComboBox);
            this.sGC_Axis.PrimaryGrid.Columns["HomeMode"].EditorParams = new object[] { homeModeArray };

            this.sGC_Axis.PrimaryGrid.Columns["Type"].EditorType = typeof(MyGridRadialMenuEditControl);
            this.sGC_Axis.PrimaryGrid.Columns["Vel"].EditorType = typeof(MyGridSliderEditControl);

            this.sGC_AxisStatus.PrimaryGrid.Columns["Status"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_AxisStatus.PrimaryGrid.Columns["Status"].EditorParams = new object[] { iL_Axis, ImageSizeMode.CenterImage };
                               
            this.sGC_AxisStatus.PrimaryGrid.Columns["Move+"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_AxisStatus.PrimaryGrid.Columns["Move+"].EditorParams = new object[] { iL_Move, ImageSizeMode.CenterImage };
                               
            this.sGC_AxisStatus.PrimaryGrid.Columns["Move-"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_AxisStatus.PrimaryGrid.Columns["Move-"].EditorParams = new object[] { iL_Move, ImageSizeMode.CenterImage };
        }
        private void sGC_Axis_PostRenderCell(object sender, GridPostRenderCellEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("Vel"))
            {
                RenderSliderBackground(e);
            }
        }
        private void RenderSliderBackground(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (Convert.ToDouble(e.GridCell.Value.ToString()) > 850)
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Red, 0f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (Convert.ToDouble(e.GridCell.Value.ToString()) <= 850 && Convert.ToDouble(e.GridCell.Value.ToString()) > 150)
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.LimeGreen, 0f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (Convert.ToDouble(e.GridCell.Value.ToString()) <= 150)
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Yellow, 0f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void RenderButtonBackground(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (e.GridCell.Value.ToString().Equals("OFF"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Orange, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.LimeGreen, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void RenderButtonBackground2(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (e.GridCell.Value.ToString().Equals("HomeFinish"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Lime, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (e.GridCell.Value.ToString().Equals("NotHome"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Gray, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (e.GridCell.Value.ToString().Equals("Homing..."))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Yellow, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (e.GridCell.Value.ToString().Equals("TimeOut"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.OrangeRed, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.LimeGreen, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void RenderButtonBackground3(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (e.GridCell.Value.ToString().Equals("Stop"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Red, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void RenderButtonBackground4(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (e.GridCell.Value.ToString().Equals("Reset"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Orange, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void sGC_Output_DragOver(object sender, DragEventArgs e)
        {
            SuperGridControl sg = sender as SuperGridControl;

            if (sg != null)
            {
                ClearDragHighlight();

                e.Effect = DragDropEffects.None;

                Point clientPoint = sg.PointToClient(new Point(e.X, e.Y));
                GridElement item = sg.GetElementAt(clientPoint.X, clientPoint.Y);

                if (item is GridCell)
                {
                    if (_dragSourceElement is GridRow)
                    {
                        item = ((GridCell)item).GridRow;
                    }
                    else if (_dragSourceElement == null || _dragSourceElement is GridCell)
                    {
                        _dragOverCell = (GridCell)item;
                        _dragOverCell.CellStyles.Default.Background = new Background(Color.AliceBlue);

                        e.Effect = DragDropEffects.Copy | DragDropEffects.Move;
                    }
                }

                if (item is GridRow)
                {
                    if (_dragSourceElement == null || _dragSourceElement is GridRow)
                    {
                        _dragOverRow = (GridRow)item;
                        _dragOverRow.CellStyles.Default.Background = new Background(Color.PaleGreen);

                        e.Effect = DragDropEffects.Copy | DragDropEffects.Move;
                    }
                }
                else if (item is GridColumnHeader)
                {
                    if (_dragSourceElement is GridColumn)
                    {
                        GridColumn doColumn = ((GridColumnHeader)item).GetHitColumn(clientPoint);

                        if (((GridColumn)_dragSourceElement).EditorType == doColumn.EditorType)
                        {
                            _dragOverColumn = doColumn;
                            _dragOverColumn.CellStyles.Default.Background = new Background(Color.AliceBlue);

                            e.Effect = DragDropEffects.Copy | DragDropEffects.Move;
                        }
                    }
                    else if (_dragSourceElement == null || _dragSourceElement is GridRow)
                    {
                        e.Effect = DragDropEffects.Copy | DragDropEffects.Move;
                    }
                }

                if (sg != _dragSourceGrid)
                {
                    e.Effect &= ~(DragDropEffects.Move);
                }
                else
                {
                    if ((e.KeyState & 8) != 8)
                        e.Effect &= ~(DragDropEffects.Copy);
                }
            }
        }
        private void ClearDragHighlight()
        {
            if (_dragOverRow != null)
                _dragOverRow.CellStyles.Default.Background = null;

            if (_dragOverCell != null)
                _dragOverCell.CellStyles.Default.Background = null;

            if (_dragOverColumn != null)
                _dragOverColumn.CellStyles.Default.Background = null;
        }
        private void ClearStyles(SuperGridControl sgrid)
        {
            GridPanel panel = sgrid.PrimaryGrid;

            foreach (GridRow row in panel.Rows)
            {
                foreach (GridCell cell in row.Cells)
                    cell.CellStyles.Default = null;
            }
        }
        private void sGC_Output_DragLeave(object sender, EventArgs e)
        {
            ClearDragHighlight();
        }

        private void sGC_Output_DragDrop(object sender, DragEventArgs e)
        {
            SuperGridControl sg = sender as SuperGridControl;

            if (sg != null)
            {
                ClearStyles(sg);

                Point pt = sg.PointToClient(new Point(e.X, e.Y));
                GridElement item = sg.GetElementAt(pt.X, pt.Y);
                if (item is GridCell)
                {
                    if ((item as GridCell).GridColumn == null)
                        return;
                    if ((item as GridCell).GridColumn.Name.Equals("Origin") ||
                       (item as GridCell).GridColumn.Name.Equals("Move"))
                    {
                        if (_dragSourceGrid != null)
                        {
                            SelectedElementCollection items =
                                (SelectedElementCollection)e.Data.GetData(typeof(SelectedElementCollection));

                            if (item is GridCell)
                            {
                                if (_dragSourceElement is GridRow)
                                    DropSgCell((GridCell)item, items);
                            }
                        }
                    }
                    ClearDragHighlight();
                }
            }
        }

        private void DropSgCell(GridCell cell, IEnumerable<GridElement> rows)
        {
            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(cell.Value.ToString()))
            {
                sb.Append(cell.Value.ToString());
                sb.Append(",");
            }
            int index = 0;
            foreach (GridRow droppedRow in rows)
            {
                if (droppedRow.Cells[0].Value != null && index < rows.Count() - 1)
                {
                    sb.Append(droppedRow.Cells[0].Value.ToString());
                    sb.Append(",");
                }
                else if(droppedRow.Cells[0].Value != null)
                {
                    sb.Append(droppedRow.Cells[0].Value.ToString());
                }
                index++;
            }

            cell.Value = sb.ToString();
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditInput"))
            {
                Sensor sensor = new Sensor();
                sensor.Description = GetSensorRandomName();
                sensor.Module = ModuleName;

                AddDigitalInput(sensor.Description, false, "", "", true, sensor);
                SingletonProvider<MachineResource>.Instance.AddSensor(sensor.Description, sensor);
            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditOutput"))
            {
                Cylinder cylinder = new Cylinder(GetCylinderRandomName());
                cylinder.Module = ModuleName;

                AddDigitalOutput(cylinder.Description, false, false, "", "", cylinder);
                SingletonProvider<MachineResource>.Instance.AddCylinder(cylinder.Description, cylinder);
            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditAxis"))
            {
                Axis axis = new Axis();
                axis.Description = GetAxisRandomName();
                axis.Module = ModuleName;

                AddAxis(axis.Description, 0.0, "ALM", "", "", true, 1, 0, false, 100, 0, 0, 0.01, 0.01, 0.05, axis);
                SingletonProvider<MachineResource>.Instance.AddAxis(axis.Description, axis);
            }
        }

        private void 控制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditInput"))
            {

            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditOutput"))
            {
                //if (sGC_Output.GetSelectedRows().Count == 0)
                //    return;
                //if(_cylinderForm == null || _cylinderForm.IsDisposed)
                //{
                //    _cylinderForm = new FormCylinder(sGC_Output.GetSelectedRows()[0].Tag as Cylinder);
                //    _cylinderForm.StartPosition = FormStartPosition.CenterParent;
                //    _cylinderForm.Show();
                //}
                //else
                //{
                //    _cylinderForm.BringToFront();
                //}
            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditAxis"))
            {
                
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditInput"))
            {
                if(sGC_Input.GetSelectedRows().Count == 0)
                {
                    MessageBox.Show("请选中需要删除的对象", "删除", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if(MessageBox.Show("确定要删除该对象吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    string sensorName = (sGC_Input.GetSelectedRows()[0] as GridRow).Cells[0].Value.ToString();
                    SingletonProvider<MachineResource>.Instance.RemoveSensor(sensorName);
                    foreach (var cylinder in MachineResource.Instance.MachineCylinder.Values)
                    {
                        if(cylinder.OriginNames.Contains(sensorName))
                            cylinder.OriginNames.Remove(sensorName);
                        foreach (var item in sGC_Output.PrimaryGrid.Rows)
                        {
                            if((item as GridRow).Tag == cylinder)
                            {
                                (item as GridRow).Cells[5].Value = string.Join(",", cylinder.OriginNames);
                                (item as GridRow).Cells[6].Value = string.Join(",", cylinder.MoveNames);
                                break;
                            }
                        }
                    }
                    int rowIndex = sGC_Input.PrimaryGrid.Rows.IndexOf(sGC_Input.GetSelectedRows()[0]);
                    sGC_Input.PrimaryGrid.Rows.Remove(sGC_Input.GetSelectedRows()[0]);
                    sGC_InputStatus.PrimaryGrid.Rows.RemoveAt(rowIndex);
                    MotionFactory.Instance.InitializeMotion2(MachineResource.Instance);
                }
            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditOutput"))
            {
                if (sGC_Output.GetSelectedRows().Count == 0)
                {
                    MessageBox.Show("请选中需要删除的对象", "删除", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确定要删除该对象吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    SingletonProvider<MachineResource>.Instance.RemoveCylinder((sGC_Output.GetSelectedRows()[0] as GridRow).Cells[0].Value.ToString());
                    int rowIndex = sGC_Output.PrimaryGrid.Rows.IndexOf(sGC_Output.GetSelectedRows()[0]);
                    sGC_Output.PrimaryGrid.Rows.Remove(sGC_Output.GetSelectedRows()[0]);
                    sGC_OutputStatus.PrimaryGrid.Rows.RemoveAt(rowIndex);
                }
            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditAxis"))
            {
                if (sGC_Axis.GetSelectedRows().Count == 0)
                {
                    MessageBox.Show("请选中需要删除的对象", "删除", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确定要删除该对象吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    SingletonProvider<MachineResource>.Instance.RemoveAxis((sGC_Axis.GetSelectedRows()[0] as GridRow).Cells[0].Value.ToString());
                    int rowIndex = sGC_Axis.PrimaryGrid.Rows.IndexOf(sGC_Axis.GetSelectedRows()[0]);
                    sGC_Axis.PrimaryGrid.Rows.Remove(sGC_Axis.GetSelectedRows()[0]);
                    sGC_AxisStatus.PrimaryGrid.Rows.RemoveAt(rowIndex);
                }
            }
        }

        private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditInput"))
            {
            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditOutput"))
            {

            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditAxis"))
            {

            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditInput"))
            {
            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditOutput"))
            {

            }
            else if ((sender as ToolStripMenuItem).Owner.Name.Equals("cMS_EditAxis"))
            {

            }
        }
        public bool VerifyConfig()
        {
            //检查Input
            List<string> moduleSensorTempNames = new List<string>();
            int index = 0;
            foreach (GridRow item in sGC_Input.PrimaryGrid.Rows)
            {
                if(string.IsNullOrEmpty(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输入配置存在错误:{"名称不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[1].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输入配置存在错误:{"来源不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[2].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输入配置存在错误:{"ID不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (moduleSensorTempNames.Contains(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输入配置存在错误:{"已存在同名对象!"},第{index + 1}行", true);
                    return false;
                }
                else
                    moduleSensorTempNames.Add(item.Cells[0].Value.ToString());
                index++;
            }
            //检查Output
            List<string> moduleCylinderTempNames = new List<string>();
            index = 0;
            foreach (GridRow item in sGC_Output.PrimaryGrid.Rows)
            {
                if (string.IsNullOrEmpty(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输出配置存在错误:{"名称不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[1].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输出配置存在错误:{"来源不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[2].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输出配置存在错误:{"ID不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (moduleCylinderTempNames.Contains(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"数字量输出配置存在错误:{"已存在同名对象!"},第{index + 1}行", true);
                    return false;
                }
                else
                    moduleCylinderTempNames.Add(item.Cells[0].Value.ToString());
                index++;
            }
            //检查Axis
            List<string> moduleAxisTempNames = new List<string>();
            index = 0;
            foreach (GridRow item in sGC_Axis.PrimaryGrid.Rows)
            {
                if (string.IsNullOrEmpty(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"轴配置存在错误:{"名称不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[1].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"轴配置存在错误:{"来源不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (string.IsNullOrEmpty(item.Cells[2].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"轴配置存在错误:{"ID不能为空!"},第{index + 1}行", true);
                    return false;
                }
                if (moduleAxisTempNames.Contains(item.Cells[0].Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"轴配置存在错误:{"已存在同名对象!"},第{index + 1}行", true);
                    return false;
                }
                else
                    moduleAxisTempNames.Add(item.Cells[0].Value.ToString());
                index++;
            }
            return true;
        }
        public void SaveConfig()
        {
            //保存Input
            int index = 0;
            foreach (GridRow item in sGC_Input.PrimaryGrid.Rows)
            {
                Sensor sensor = new Sensor(item.Cells[0].Value.ToString(),
                                           item.Cells[2].Value.ToString(),
                                           item.Cells[3].Value.ToString(),
                                           !Convert.ToBoolean(item.Cells[4].Value.ToString()),
                                           ModuleName);
                SingletonProvider<MachineResource>.Instance.AddSensor(sensor.Description, sensor);
                index++;
            }
            //保存output
            index = 0;
            foreach (GridRow item in sGC_Output.PrimaryGrid.Rows)
            {
                Cylinder cylinder = new Cylinder(item.Cells[0].Value.ToString(),
                                                 item.Cells[3].Value.ToString(),
                                                 item.Cells[4].Value.ToString(),
                                                 ModuleName);
                if(!string.IsNullOrEmpty(item.Cells[5].Value.ToString()))
                {
                    cylinder.ControlCardName2 = item.Cells[5].Value.ToString();
                    cylinder.ID2 = item.Cells[6].Value.ToString();
                }
                if(!string.IsNullOrEmpty(item.Cells[7].Value.ToString()))
                {
                    cylinder.OriginNames = item.Cells[7].Value.ToString().Split(',').ToList();
                }
                if (!string.IsNullOrEmpty(item.Cells[8].Value.ToString()))
                {
                    cylinder.MoveNames = item.Cells[8].Value.ToString().Split(',').ToList();
                }
                SingletonProvider<MachineResource>.Instance.AddCylinder(cylinder.Description, cylinder);
                index++;
            }
            //保存axis
            index = 0;
            foreach (GridRow item in sGC_Axis.PrimaryGrid.Rows)
            {
                Axis axis = new Axis(item.Cells[3].Value.ToString(),
                                     item.Cells[4].Value.ToString(),
                                     !Convert.ToBoolean(item.Cells[5].Value.ToString()),
                                     item.Cells[0].Value.ToString(),
                                     ModuleName);
                axis.ConvertRate = Convert.ToInt32(item.Cells[6].Value.ToString());
                axis.HomeMode = Convert.ToInt32(item.Cells[7].Value.ToString());
                axis.HomeOffset = Convert.ToDouble(item.Cells[8].Value.ToString());
                SingletonProvider<MachineResource>.Instance.AddAxis(axis.Description, axis);
                index++;
            }
        }
        public string GetSensorRandomName()
        {
            for (int i = 0; i < 999999; i++)
            {
                if (SingletonProvider<MachineResource>.Instance.MachineSensor.ContainsKey($"Input{i}"))
                    continue;
                else
                    return $"Input{i}";
            }
            return null;
        }
        public string GetCylinderRandomName()
        {
            for (int i = 0; i < 999999; i++)
            {
                if (SingletonProvider<MachineResource>.Instance.MachineCylinder.ContainsKey($"Output{i}"))
                    continue;
                else
                    return $"Output{i}";
            }
            return null;
        }
        public string GetAxisRandomName()
        {
            for (int i = 0; i < 999999; i++)
            {
                if (SingletonProvider<MachineResource>.Instance.MachineAxis.ContainsKey($"Axis{i}"))
                    continue;
                else
                    return $"Axis{i}";
            }
            return null;
        }
        //bool testFlag = false;
        private void sGC_Axis_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            Axis axisOld = e.GridCell.GridRow.Tag as Axis;
            //修改名字
            if (e.GridCell.GridColumn.Name.Equals("Description"))
            {
                if (MachineResource.Instance.MachineAxis.ContainsKey(e.GridCell.Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"已存在同名轴:{e.GridCell.Value},请使用其他名称", true);
                    e.GridCell.Value = axisOld.Description;
                    return;
                }
                string oldName = axisOld.Description;
                axisOld.Description = e.GridCell.Value.ToString();
                SingletonProvider<MachineResource>.Instance.RemoveAxis(oldName);
                SingletonProvider<MachineResource>.Instance.AddAxis(axisOld.Description, axisOld);
                if(!string.IsNullOrEmpty(axisOld.ControlCardName) &&
                   !string.IsNullOrEmpty(axisOld.ID))
                    MotionFactory.Instance.InitializeAxis(SingletonProvider<MachineResource>.Instance, axisOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Source"))
            {
                (e.GridCell.GridRow.Tag as Axis).ControlCardName = e.GridCell.Value.ToString();
                if (!string.IsNullOrEmpty(axisOld.ControlCardName) &&
                   !string.IsNullOrEmpty(axisOld.ID))
                    MotionFactory.Instance.InitializeAxis(SingletonProvider<MachineResource>.Instance, axisOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("ID"))
            {
                (e.GridCell.GridRow.Tag as Axis).ID = e.GridCell.Value.ToString();
                if (!string.IsNullOrEmpty(axisOld.ControlCardName) &&
                   !string.IsNullOrEmpty(axisOld.ID))
                    MotionFactory.Instance.InitializeAxis(SingletonProvider<MachineResource>.Instance, axisOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Encoder"))
            {
                (e.GridCell.GridRow.Tag as Axis).IsStep = !Convert.ToBoolean(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.GridColumn.Name.Equals("Pulse/MM"))
            {
                (e.GridCell.GridRow.Tag as Axis).ConvertRate = Convert.ToDouble(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.GridColumn.Name.Equals("HomeMode"))
            {
                (e.GridCell.GridRow.Tag as Axis).HomeMode = Convert.ToInt32(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.GridColumn.Name.Equals("HomeDirection"))
            {
                (e.GridCell.GridRow.Tag as Axis).HomeDirP = Convert.ToBoolean(e.GridCell.Value.ToString());
            }
            else if (e.GridCell.GridColumn.Name.Equals("Type"))
            {
                int rowIndex = e.GridCell.RowIndex;
                GridRow gr = (sGC_Axis.GetSelectedRows()[0] as GridRow);
                if(e.GridCell.Value.ToString() == "Auto Vel")
                {
                    gr.Cells[7].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[0];
                    gr.Cells[8].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[0];
                    gr.Cells[9].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[0];
                    gr.Cells[10].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[0];
                    gr.Cells[11].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[0];
                    gr.Cells[12].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[0];
                }
                else if (e.GridCell.Value.ToString() == "Manual Vel")
                {
                    gr.Cells[7].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[1];
                    gr.Cells[8].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[1];
                    gr.Cells[9].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[1];
                    gr.Cells[10].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[1];
                    gr.Cells[11].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[1];
                    gr.Cells[12].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[1];
                }
                else if (e.GridCell.Value.ToString() == "Home Vel")
                {
                    gr.Cells[7].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2];
                    gr.Cells[8].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2];
                    gr.Cells[9].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2];
                    gr.Cells[10].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2];
                    gr.Cells[11].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2];
                    gr.Cells[12].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[2];
                }
                else if (e.GridCell.Value.ToString() == "Try Vel")
                {
                    gr.Cells[7].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[3];
                    gr.Cells[8].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[3];
                    gr.Cells[9].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[3];
                    gr.Cells[10].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[3];
                    gr.Cells[11].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[3];
                    gr.Cells[12].Value = SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[3];
                }
            }
            else if (e.GridCell.GridColumn.Name.Equals("Vel"))
            {
                if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Auto Vel"))
                {
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[7].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Manual Vel"))
                {
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[7].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Home Vel"))
                {
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[7].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Try Vel"))
                {
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[3] = Convert.ToDouble(e.GridCell.GridRow.Cells[7].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
            }
            else if (e.GridCell.GridColumn.Name.Equals("Start"))
            {
                if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Auto Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[8].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Manual Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[8].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Home Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[8].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Try Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[3] = Convert.ToDouble(e.GridCell.GridRow.Cells[8].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
            }
            else if (e.GridCell.GridColumn.Name.Equals("End"))
            {
                if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Auto Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Manual Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Home Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Try Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[3] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
            }
            else if (e.GridCell.GridColumn.Name.Equals("Acc"))
            {
                if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Auto Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Manual Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Home Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Try Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[3] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[13].Value.ToString());
                }
            }
            else if (e.GridCell.GridColumn.Name.Equals("Dec"))
            {
                if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Auto Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Manual Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Home Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Try Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingDec[3] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                }
            }
            else if (e.GridCell.GridColumn.Name.Equals("STime"))
            {
                if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Auto Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[0] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Manual Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[1] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Home Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                }
                else if (e.GridCell.GridRow.Cells[13].Value.ToString().Equals("Try Vel"))
                {
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVel[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[9].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelStart[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[10].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingVelEnd[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[11].Value.ToString());
                    //SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingAcc[2] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                    SingletonProvider<MachineResource>.Instance.MachineAxis[e.GridCell.GridRow.Cells[0].Value.ToString()].SettingSTime[3] = Convert.ToDouble(e.GridCell.GridRow.Cells[12].Value.ToString());
                }
            }
        }

        private void sGC_Input_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            Sensor sensorOld = e.GridCell.GridRow.Tag as Sensor;
            //修改名字
            if (e.GridCell.ColumnIndex == 0)
            {
                if (MachineResource.Instance.MachineSensor.ContainsKey(e.GridCell.Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"已存在同名输入:{e.GridCell.Value},请使用其他名称", true);
                    e.GridCell.Value = sensorOld.Description;
                    return;
                }
                string oldName = sensorOld.Description;
                sensorOld.Description = e.GridCell.Value.ToString();
                SingletonProvider<MachineResource>.Instance.RemoveSensor(oldName);
                SingletonProvider<MachineResource>.Instance.AddSensor(sensorOld.Description, sensorOld);
                if (string.IsNullOrEmpty(sensorOld.ControlCardName) || string.IsNullOrEmpty(sensorOld.ID))
                    return;
                MotionFactory.Instance.InitializeSensor(SingletonProvider<MachineResource>.Instance, sensorOld);
            }
            else if(e.GridCell.ColumnIndex == 1)
            {
                (e.GridCell.GridRow.Tag as Sensor).ControlCardName = e.GridCell.Value.ToString();
                if (string.IsNullOrEmpty(sensorOld.ControlCardName) || string.IsNullOrEmpty(sensorOld.ID))
                    return;
                MotionFactory.Instance.InitializeSensor(SingletonProvider<MachineResource>.Instance, sensorOld);
            }
            else if (e.GridCell.ColumnIndex == 2)
            {
                (e.GridCell.GridRow.Tag as Sensor).ID = e.GridCell.Value.ToString();
                if (string.IsNullOrEmpty(sensorOld.ControlCardName) || string.IsNullOrEmpty(sensorOld.ID))
                    return;
                MotionFactory.Instance.InitializeSensor(SingletonProvider<MachineResource>.Instance, sensorOld);
            }
            else if (e.GridCell.ColumnIndex == 3)
            {
                (e.GridCell.GridRow.Tag as Sensor).AlwaysOn = !Convert.ToBoolean(e.GridCell.Value.ToString());
                if (string.IsNullOrEmpty(sensorOld.ControlCardName) || string.IsNullOrEmpty(sensorOld.ID))
                    return;
                MotionFactory.Instance.InitializeSensor(SingletonProvider<MachineResource>.Instance, sensorOld);
            }
        }

        private void sGC_Output_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            Cylinder cylinderOld = e.GridCell.GridRow.Tag as Cylinder;
            int rowIndex = e.GridCell.RowIndex;
            //修改名字
            if (e.GridCell.ColumnIndex == 0)
            {
                if (MachineResource.Instance.MachineCylinder.ContainsKey(e.GridCell.Value.ToString()))
                {
                    SystemInfo.ShowToastInfo(this, $"已存在同名输出:{e.GridCell.Value},请使用其他名称", true);
                    e.GridCell.Value = cylinderOld.Description;
                    return;
                }
                string oldName = cylinderOld.Description;
                cylinderOld.Description = e.GridCell.Value.ToString();
                SingletonProvider<MachineResource>.Instance.RemoveCylinder(oldName);
                SingletonProvider<MachineResource>.Instance.AddCylinder(cylinderOld.Description, cylinderOld);
                if (string.IsNullOrEmpty(cylinderOld.ControlCardName1) || string.IsNullOrEmpty(cylinderOld.ID1))
                    return;
                MotionFactory.Instance.InitializeCylinder(SingletonProvider<MachineResource>.Instance, cylinderOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Source"))
            {
                (e.GridCell.GridRow.Tag as Cylinder).ControlCardName1 = e.GridCell.Value.ToString();
                if (string.IsNullOrEmpty(cylinderOld.ControlCardName1) || string.IsNullOrEmpty(cylinderOld.ID1))
                    return;
                MotionFactory.Instance.InitializeCylinder(SingletonProvider<MachineResource>.Instance, cylinderOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("ID"))
            {
                (e.GridCell.GridRow.Tag as Cylinder).ID1 = e.GridCell.Value.ToString();
                if (string.IsNullOrEmpty(cylinderOld.ControlCardName1) || string.IsNullOrEmpty(cylinderOld.ID1))
                    return;
                MotionFactory.Instance.InitializeCylinder(SingletonProvider<MachineResource>.Instance, cylinderOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Source2"))
            {
                (e.GridCell.GridRow.Tag as Cylinder).ControlCardName2 = e.GridCell.Value.ToString();
                if (string.IsNullOrEmpty(cylinderOld.ControlCardName1) || string.IsNullOrEmpty(cylinderOld.ID1))
                    return;
                MotionFactory.Instance.InitializeCylinder(SingletonProvider<MachineResource>.Instance, cylinderOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("ID2"))
            {
                (e.GridCell.GridRow.Tag as Cylinder).ID2 = e.GridCell.Value.ToString();
                if (string.IsNullOrEmpty(cylinderOld.ControlCardName1) || string.IsNullOrEmpty(cylinderOld.ID1))
                    return;
                MotionFactory.Instance.InitializeCylinder(SingletonProvider<MachineResource>.Instance, cylinderOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Origin"))
            {
                Cylinder cylinder = e.GridCell.GridRow.Tag as Cylinder;
                cylinder.OriginNames = e.GridCell.Value.ToString().Split(',').ToList();
                cylinder.Origins.Clear();
                foreach (var item in cylinder.OriginNames)
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineSensor.Keys.Contains(item))
                        cylinder.Origins.Add(SingletonProvider<MachineResource>.Instance.MachineSensor[item]);
                }
                if (string.IsNullOrEmpty(cylinderOld.ControlCardName1) || string.IsNullOrEmpty(cylinderOld.ID1))
                    return;
                MotionFactory.Instance.InitializeCylinder(SingletonProvider<MachineResource>.Instance, cylinderOld);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Move"))
            {
                Cylinder cylinder = e.GridCell.GridRow.Tag as Cylinder;
                cylinder.MoveNames = e.GridCell.Value.ToString().Split(',').ToList();
                cylinder.Moves.Clear();
                foreach (var item in cylinder.MoveNames)
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineSensor.Keys.Contains(item))
                        cylinder.Moves.Add(SingletonProvider<MachineResource>.Instance.MachineSensor[item]);
                }
                if (string.IsNullOrEmpty(cylinderOld.ControlCardName1) || string.IsNullOrEmpty(cylinderOld.ID1))
                    return;
                MotionFactory.Instance.InitializeCylinder(SingletonProvider<MachineResource>.Instance, cylinderOld);
            }
        }

        private void sGC_Axis_CellClick(object sender, GridCellClickEventArgs e)
        {
            
        }
        public void LoadLayout()
        {
            if (this.ContainerSplitDistance != null && this.ContainerSplitDistance.Count > 0)
            {
                if(this.sC_UpDown.Height > this.ContainerSplitDistance[0])
                    this.sC_UpDown.SplitterDistance = this.ContainerSplitDistance[0];
                this.sC_LeftRight.SplitterDistance = this.ContainerSplitDistance[1];
                this.sC_Input.SplitterDistance = this.ContainerSplitDistance[2];
                this.sC_Output.SplitterDistance = this.ContainerSplitDistance[3];
                this.sC_Axis.SplitterDistance = this.ContainerSplitDistance[4];
            }
            else
            {
                if(this.sC_LeftRight.Width > 0)
                    this.sC_LeftRight.SplitterDistance = this.sC_LeftRight.Width / 2;
                if(this.sC_UpDown.Height > 0)
                    this.sC_UpDown.SplitterDistance = this.sC_UpDown.Height * 3 / 5;
            }
        }
        public void SaveLayout()
        {
            this.ContainerSplitDistance.Clear();
            this.ContainerSplitDistance.Add(sC_UpDown.SplitterDistance);
            this.ContainerSplitDistance.Add(sC_LeftRight.SplitterDistance);
            this.ContainerSplitDistance.Add(sC_Input.SplitterDistance);
            this.ContainerSplitDistance.Add(sC_Output.SplitterDistance);
            this.ContainerSplitDistance.Add(sC_Axis.SplitterDistance);
        }
        private void t_IO_Tick(object sender, EventArgs e)
        {
#if DEBUGWITHOUTMACHINE
#else
            lock (GlobalVariable.SaveConfigLock)
            {
                foreach (var row in sGC_AxisStatus.PrimaryGrid.Rows)
                {
                    Axis axis = (row.Tag as Axis);
                    (row as GridRow).Cells[0].Value = axis.ServoOn ? "ON" : "OFF";
                    (row as GridRow).Cells[1].Value = axis.Position.ToString("0.000");

                    if (axis.ServoWarn)
                        (row as GridRow).Cells[2].Value = "ALM.png";
                    else if (axis.Origin)
                        (row as GridRow).Cells[2].Value = "ORG.png";
                    else if (axis.PositiveLimit)
                        (row as GridRow).Cells[2].Value = "PEL.png";
                    else if (axis.NegativeLimit)
                        (row as GridRow).Cells[2].Value = "NEL.png";
                    else if (axis.Ready)
                        (row as GridRow).Cells[2].Value = "RDY.png";
                    else if (!axis.Ready)
                        (row as GridRow).Cells[2].Value = "MOV.png";

                    //(row as GridRow).Cells[3].Value = axis.Home ? "HomeFinish" : (row as GridRow).Cells[3].Value;
                }
                foreach (var row in sGC_OutputStatus.PrimaryGrid.Rows)
                {
                    Cylinder cylinder = (row.Tag as Cylinder);
                    if (cylinder.Card1 == null || string.IsNullOrEmpty(cylinder.ID1))
                        continue;
                    (row as GridRow).Cells[0].Value = cylinder.AlreadyOn() ? "圆形选中32.png" : "圆形未选中32.png";
                    (row as GridRow).Cells[1].Value = cylinder.AlreadyOn();
                    if (cylinder.Origins != null && cylinder.Origins.Count > 0)
                        (row as GridRow).Cells[2].Value = cylinder.InOrigin() ? "圆形选中32.png" : "圆形未选中32.png";
                    if (cylinder.Moves != null && cylinder.Moves.Count > 0)
                        (row as GridRow).Cells[3].Value = cylinder.InMove() ? "圆形选中32.png" : "圆形未选中32.png";
                }
                foreach (var row in sGC_InputStatus.PrimaryGrid.Rows)
                {
                    Sensor sensor = (row.Tag as Sensor);
                    if (sensor.Card == null || string.IsNullOrEmpty(sensor.ID))
                        continue;
                    (row as GridRow).Cells[0].Value = sensor.WaitOn() ? "圆形选中32.png" : "圆形未选中32.png";
                }
            }
#endif
        }

        private void sGC_Output_SelectionChanged(object sender, GridEventArgs e)
        {
        }

        private void sGC_AxisStatus_PostRenderCell(object sender, GridPostRenderCellEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("ServoON"))
            {
                RenderButtonBackground(e);
            }
            else if (e.GridCell.GridColumn.Name.Equals("GoHome"))
            {
                RenderButtonBackground2(e);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Stop"))
            {
                RenderButtonBackground3(e);
            }
            else if (e.GridCell.GridColumn.Name.Equals("WarnReset"))
            {
                RenderButtonBackground4(e);
            }
        }

        private void sGC_OutputStatus_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            Cylinder cylinderOld = e.GridCell.GridRow.Tag as Cylinder;

            if (e.GridCell.GridColumn.Name.Equals("Trigger"))
            {
                if (Convert.ToBoolean(e.GridCell.Value))
                {
                    bool isOK = false;
                    string errInfo = "";
                    (isOK, errInfo) = cylinderOld.On();
                    if (!isOK)
                    {
                        SystemInfo.ShowToastInfo(this, errInfo, true);
                    }
                }
                else
                {
                    bool isOK = false;
                    string errInfo = "";
                    (isOK, errInfo) = cylinderOld.Off();
                    if (!isOK)
                    {
                        SystemInfo.ShowToastInfo(this, errInfo, true);
                    }
                }
            }
        }

        private void sC_SplitterMoved(object sender, SplitterEventArgs e)
        {
            
        }

        private void sGC_AxisStatus_CellMouseDown(object sender, GridCellMouseEventArgs e)
        {
            Axis axis = e.GridCell.GridRow.Tag as Axis;
            string stopReason = "";
            if (e.GridCell.GridColumn.Name.Equals("Move+"))
            {
                axis.SetSpeed(SpeedType.Manual);
                if (!axis.MoveContinuous(true, out stopReason))
                    SystemInfo.ShowToastInfo(this, stopReason, true);
                e.GridCell.Value = "右2.png";
            }
            else if (e.GridCell.GridColumn.Name.Equals("Move-"))
            {
                axis.SetSpeed(SpeedType.Manual);
                if(!axis.MoveContinuous(false, out stopReason))
                    SystemInfo.ShowToastInfo(this, stopReason, true);
                e.GridCell.Value = "左2.png";
            }
            else if (e.GridCell.GridColumn.Name.Equals("Stop"))
            {
                //axis.MoveContinuous(true);
                e.GridCell.CellStyles.Default.TextColor = Color.Black;
            }
        }

        private void sGC_AxisStatus_CellMouseUp(object sender, GridCellMouseEventArgs e)
        {
            Axis axis = e.GridCell.GridRow.Tag as Axis;
            if (e.GridCell.GridColumn.Name.Equals("Move+"))
            {
                axis.Stop(false);
                e.GridCell.Value = "右1.png";
            }
            else if (e.GridCell.GridColumn.Name.Equals("Move-"))
            {
                axis.Stop(false);
                e.GridCell.Value = "左1.png";
            }
            else if (e.GridCell.GridColumn.Name.Equals("Stop"))
            {
                //axis.MoveContinuous(true);
                e.GridCell.CellStyles.Default.TextColor = Color.White;
            }
        }
        private bool homeConditionCheck(Axis axis)
        {
            #region 移栽
            if (axis.Description.Equals("直线电机左"))
            {
                if (MachineResource.Instance.MachineAxis["测试Y轴左"].Position >= 2)
                {
                    SystemInfo.ShowToastInfo(this, $"测试Y轴左不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (MachineResource.Instance.MachineAxis["测试Z轴左"].Position > (MachineResource.Instance.MachineAxis["测试Z轴左"].MotionPosition["安全位置"] + 2))
                {
                    SystemInfo.ShowToastInfo(this, $"测试Z轴左不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (!MachineResource.Instance.MachineSensor["左测试移载下料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载下料上下气缸2上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载上料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载上料上下气缸2上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("测试X轴左"))
            {
                if (MachineResource.Instance.MachineAxis["测试Y轴左"].Position >= 2)
                {
                    SystemInfo.ShowToastInfo(this, $"测试Y轴左不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (MachineResource.Instance.MachineAxis["测试Z轴左"].Position > (MachineResource.Instance.MachineAxis["测试Z轴左"].MotionPosition["安全位置"] + 2))
                {
                    SystemInfo.ShowToastInfo(this, $"测试Z轴左不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (!MachineResource.Instance.MachineSensor["左测试移载下料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载下料上下气缸2上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载上料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载上料上下气缸2上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("测试Y轴左"))
            {
                if (!MachineResource.Instance.MachineSensor["左测试移载下料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载下料上下气缸2上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载上料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["左测试移载上料上下气缸2上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("测试Z轴左"))
            {
                if (MachineResource.Instance.MachineAxis["测试Y轴左"].Position >= 2)
                {
                    SystemInfo.ShowToastInfo(this, $"测试Y轴左不在安全位置，无法启动回零!", true);
                    return false;
                }
            }

            if (axis.Description.Equals("直线电机右"))
            {
                if (MachineResource.Instance.MachineAxis["测试Y轴右"].Position >= 2)
                {
                    SystemInfo.ShowToastInfo(this, $"测试Y轴右不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (MachineResource.Instance.MachineAxis["测试Z轴右"].Position > (MachineResource.Instance.MachineAxis["测试Z轴右"].MotionPosition["安全位置"] + 2))
                {
                    SystemInfo.ShowToastInfo(this, $"测试Z轴右不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (!MachineResource.Instance.MachineSensor["右测试移载下料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载下料上下气缸2上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载上料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载上料上下气缸2上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("测试X轴右"))
            {
                if (MachineResource.Instance.MachineAxis["测试Y轴右"].Position >= 2)
                {
                    SystemInfo.ShowToastInfo(this, $"测试Y轴右不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (MachineResource.Instance.MachineAxis["测试Z轴右"].Position > (MachineResource.Instance.MachineAxis["测试Z轴右"].MotionPosition["安全位置"] + 2))
                {
                    SystemInfo.ShowToastInfo(this, $"测试Z轴右不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (!MachineResource.Instance.MachineSensor["右测试移载下料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载下料上下气缸2上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载上料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载上料上下气缸2上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("测试Y轴右"))
            {
                if (!MachineResource.Instance.MachineSensor["右测试移载下料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载下料上下气缸2上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载上料上下气缸1上位"].WaitOn() ||
                    !MachineResource.Instance.MachineSensor["右测试移载上料上下气缸2上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("测试Z轴右"))
            {
                if (MachineResource.Instance.MachineAxis["测试Y轴右"].Position >= 2)
                {
                    SystemInfo.ShowToastInfo(this, $"测试Y轴右不在安全位置，无法启动回零!", true);
                    return false;
                }
            }
            #endregion

            #region 上料四轴
            if (axis.Description.Equals("上料X轴"))
            {
                if (MachineResource.Instance.MachineAxis["上料Z轴"].Position > (MachineResource.Instance.MachineAxis["上料Z轴"].MotionPosition["安全位置"] + 2))
                {
                    SystemInfo.ShowToastInfo(this, $"上料Z轴不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (MachineResource.Instance.MachineSensor["上料四轴上下气缸1下位"].WaitOn() ||
                    MachineResource.Instance.MachineSensor["上料四轴上下气缸2下位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("上料Y轴"))
            {
                if (MachineResource.Instance.MachineAxis["上料Z轴"].Position > (MachineResource.Instance.MachineAxis["上料Z轴"].MotionPosition["安全位置"] + 2))
                {
                    SystemInfo.ShowToastInfo(this, $"上料Z轴不在安全位置，无法启动回零!", true);
                    return false;
                }
                if (MachineResource.Instance.MachineSensor["上料四轴上下气缸1下位"].WaitOn() ||
                    MachineResource.Instance.MachineSensor["上料四轴上下气缸2下位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"夹爪不在上位，无法启动回零!", true);
                    return false;
                }
            }
            #endregion

            #region 装拆假镜头
            if (axis.Description.Equals("装假镜头X轴"))
            {
                if (!MachineResource.Instance.MachineSensor["ST3上下气缸上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"吸嘴不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("装假镜头Y轴"))
            {
                if (!MachineResource.Instance.MachineSensor["ST3上下气缸上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"吸嘴不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("拆假镜头X轴"))
            {
                if (!MachineResource.Instance.MachineSensor["ST3上下气缸上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"吸嘴不在上位，无法启动回零!", true);
                    return false;
                }
            }
            if (axis.Description.Equals("拆假镜头Y轴"))
            {
                if (!MachineResource.Instance.MachineSensor["ST3上下气缸上位"].WaitOn())
                {
                    SystemInfo.ShowToastInfo(this, $"吸嘴不在上位，无法启动回零!", true);
                    return false;
                }
            }
            #endregion

            return true;
        }
        private void sGC_AxisStatus_CellClick(object sender, GridCellClickEventArgs e)
        {
            Axis axisOld = e.GridCell.GridRow.Tag as Axis;

#if DEBUGWITHOUTMACHINE
#else
            if (e.GridCell.GridColumn.Name.Equals("ServoON"))
            {
                if (e.GridCell.Value.Equals("ON"))
                    axisOld.SetServoOff();
                else
                    axisOld.SetServoOn();
            }
            else if(e.GridCell.GridColumn.Name.Equals("GoHome"))
            {
                if (e.GridCell.Value.Equals("NotHome") || e.GridCell.Value.Equals("HomeFinish") || e.GridCell.Value.Equals("TimeOut"))
                {
                    if (!homeConditionCheck(axisOld))
                        return;
                    Task.Run(() => {

                        Axis axis = axisOld;
                        Task task = new Task(new Action(() =>
                        {
                            axis.VelStart = axis.SettingVelStart[2];
                            axis.Vel = axis.SettingVel[2];
                            axis.VelEnd = axis.SettingVelEnd[2];
                            axis.Acc = axis.SettingAcc[2];
                            axis.Dec = axis.SettingDec[2];
                            axis.GoHome((short)axis.HomeMode, axis.HomeDirP);
                        }));
                        task.Start();
                        this.BeginInvoke(new Action(() =>
                        {
                            e.GridCell.Value = "Homing...";
                        }));
                        if (!task.Wait(60000))
                        {
                            axis.Stop(false);
                            this.BeginInvoke(new Action(() =>
                            {
                                e.GridCell.Value = "TimeOut";
                            }));
                            this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, $"{axis.Description}回零超时!", true);
                            }));
                        }
                        else
                        {
                            if (axis.Home)
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{axis.Description}回零完成!", false);
                                    e.GridCell.Value = "HomeFinish";
                                }));
                            }
                            else
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{axis.Description}回零失败!", true);
                                    e.GridCell.Value = "NotHome";
                                }));
                            }
                        }
                        return true;
                    });
                }
                else if(e.GridCell.Value.Equals("Homing..."))
                {
                    SystemInfo.ShowToastInfo(this, "该轴已在回零中!", true);
                }
            }
            else if(e.GridCell.GridColumn.Name.Equals("Stop"))
            {
                axisOld.Stop(false);
            }
            else if(e.GridCell.GridColumn.Name.Equals("WarnReset"))
            {
                axisOld.ClearAlarm();
            }
#endif
        }

        private void sGC_Input_Scroll(object sender, GridScrollEventArgs e)
        {
            sGC_InputStatus.VScrollOffset = sGC_Input.VScrollOffset;
            sGC_Input.Refresh();
            sGC_InputStatus.Refresh();
        }

        private void sGC_InputStatus_Scroll(object sender, GridScrollEventArgs e)
        {
            sGC_Input.VScrollOffset = sGC_InputStatus.VScrollOffset;
            sGC_Input.Refresh();
            sGC_InputStatus.Refresh();
        }

        private void sGC_Input_SelectionChanged(object sender, GridEventArgs e)
        {
            //if(sGC_Input.GetSelectedRows().Count > 0)
            //{
            //    sGC_InputStatus.PrimaryGrid.ClearSelectedRows();
            //    sGC_InputStatus.PrimaryGrid.ClearSelectedCells();
            //    int index = sGC_Input.PrimaryGrid.Rows.IndexOf(sGC_Input.GetSelectedRows()[0]);
            //    sGC_InputStatus.PrimaryGrid.Select(sGC_InputStatus.PrimaryGrid.Rows[index] as GridContainer);
            //}
        }

        private void sGC_InputStatus_SelectionChanged(object sender, GridEventArgs e)
        {
            //if (sGC_InputStatus.GetSelectedRows().Count > 0)
            //{
            //    sGC_Input.PrimaryGrid.ClearSelectedRows();
            //    sGC_Input.PrimaryGrid.ClearSelectedCells();
            //    int index = sGC_InputStatus.PrimaryGrid.Rows.IndexOf(sGC_InputStatus.GetSelectedRows()[0]);
            //    sGC_Input.PrimaryGrid.Select(sGC_Input.PrimaryGrid.Rows[index] as GridContainer);
            //}
        }

        private void sGC_Output_Scroll(object sender, GridScrollEventArgs e)
        {
            sGC_OutputStatus.VScrollOffset = sGC_Output.VScrollOffset;
            sGC_Output.Refresh();
            sGC_OutputStatus.Refresh();
        }

        private void sGC_OutputStatus_Scroll(object sender, GridScrollEventArgs e)
        {
            sGC_Output.VScrollOffset = sGC_OutputStatus.VScrollOffset;
            sGC_Output.Refresh();
            sGC_OutputStatus.Refresh();
        }

        private void sGC_Axis_Scroll(object sender, GridScrollEventArgs e)
        {
            sGC_AxisStatus.VScrollOffset = sGC_Axis.VScrollOffset;
            sGC_Axis.Refresh();
            sGC_AxisStatus.Refresh();
        }

        private void sGC_AxisStatus_Scroll(object sender, GridScrollEventArgs e)
        {
            sGC_Axis.VScrollOffset = sGC_AxisStatus.VScrollOffset;
            sGC_Axis.Refresh();
            sGC_AxisStatus.Refresh();
        }

        private void sGC_AxisStatusNew_PostRenderCell(object sender, GridPostRenderCellEventArgs e)
        {
            //if (e.GridCell.GridColumn.Name.Equals("ServoON"))
            //{
            //    RenderButtonBackground(e);
            //}
            //else if (e.GridCell.GridColumn.Name.Equals("GoHome"))
            //{
            //    RenderButtonBackground2(e);
            //}
            //else if (e.GridCell.GridColumn.Name.Equals("Stop"))
            //{
            //    RenderButtonBackground3(e);
            //}
        }

        public void ChangeEditMode(bool isEnable)
        {
            foreach (GridRow row in sGC_Input.PrimaryGrid.Rows)
            {
                foreach (GridCell cell in row.Cells)
                {
                    cell.ReadOnly = !isEnable;
                }
            }
            foreach (GridRow row in sGC_Output.PrimaryGrid.Rows)
            {
                foreach (GridCell cell in row.Cells)
                {
                    cell.ReadOnly = !isEnable;
                }
            }
            foreach (GridRow row in sGC_Axis.PrimaryGrid.Rows)
            {
                foreach (GridCell cell in row.Cells)
                {
                    cell.ReadOnly = !isEnable;
                }
            }
        }
    }
    internal class FragrantComboBox : GridComboBoxExEditControl
    {
        public FragrantComboBox(IEnumerable orderArray)
        {
            DataSource = orderArray;
        }
    }
    internal class MyGridImageEditControl : GridImageEditControl
    {
        public MyGridImageEditControl(
            ImageList imageList, ImageSizeMode sizeMode)
        {
            ImageList = imageList;
            ImageSizeMode = sizeMode;
        }
    }
    internal class MyGridRadialMenuEditControl : GridRadialMenuEditControl
    {
        public MyGridRadialMenuEditControl()
        {
            SetRadialMenu();
        }

#region SetRadialMenu

        private void SetRadialMenu()
        {
            Image = Properties.Resources.自动32;
            //SymbolSize = 13;
            Diameter = 300;

            RadialMenuItem item = new RadialMenuItem();
            item.Text = "Auto Vel";
            item.Image = Properties.Resources.自动32;
            Items.Add(item);

            item = new RadialMenuItem();
            item.Text = "Manual Vel";
            item.Image = Properties.Resources.手动32;
            Items.Add(item);

            item = new RadialMenuItem();
            item.Text = "Home Vel";
            item.Image = Properties.Resources.原点32;
            Items.Add(item);

            item = new RadialMenuItem();
            item.Text = "Try Vel";
            item.Image = Properties.Resources.medium;
            Items.Add(item);
        }

#endregion

#region InitializeContext
        public override void InitializeContext(GridCell cell, CellVisualStyle style)
        {
            base.InitializeContext(cell, style);

            if (style != null)
            {
                Color baseColor;

                RadialMenuItem item = FindItemByText(Items, EditorValue as string);

                if (item != null)
                {
                    Image = item.Image;
                    baseColor = Color.Green;
                }
                else
                {
                    //Symbol = "\uf040";
                    baseColor = Color.Empty;
                }

                if (cell.SuperGrid.NonModalEditorCell == cell)
                    baseColor = Color.Firebrick;

                Colors.RadialMenuButtonBorder = baseColor;
            }
        }

#endregion
    }
    internal class MyGridSliderEditControl : GridSliderEditControl
    {
        public MyGridSliderEditControl()
        {
            LabelVisible = true;
            Step = 1;
            Minimum = 1;
            Maximum = 1000;
        }

#region InitializeContext

        /// <summary>
        /// Handles the setting of the slider text to 
        /// match the current cell value
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="style"></param>
        public override void InitializeContext(
            GridCell cell, CellVisualStyle style)
        {
            base.InitializeContext(cell, style);

            Text = Value.ToString();
        }

#endregion

        public override bool EditorValueChanged
        {
            get { return (base.EditorValueChanged); }

            set
            {
                base.EditorValueChanged = value;

                //GridCell cell = EditorCell;

                //if (cell != null)
                //    cell.GridRow.Cells[0].Value = EditorCell.Value;
            }
        }
    }
    internal class MyGridButtonXEditControl : GridButtonXEditControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public MyGridButtonXEditControl()
        {
            // We want to be notified when the user clicks the button
            // so that we can change the underlying cell value to reflect
            // the mouse click.

            Click += MyGridButtonXEditControlClick;
        }

#region InitializeContext

        /// <summary>
        /// Initializes the color table for the button
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="style"></param>
        public override void
            InitializeContext(GridCell cell, CellVisualStyle style)
        {
            base.InitializeContext(cell, style);
        }

#endregion

#region MyGridButtonXEditControlClick

        /// <summary>
        /// Handles user clicks of the button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MyGridButtonXEditControlClick(object sender, EventArgs e)
        {
            bool running = (EditorCell.Value != null &&
                    EditorCell.Value.Equals("Path"));
            EditorCell.Value = (running == true) ? "Edit" : "Path";
            //EditorCell.Value = "Homing...";
        }

#endregion
    }
    internal class MyGridButtonXEditControl2 : GridButtonXEditControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public MyGridButtonXEditControl2()
        {
            // We want to be notified when the user clicks the button
            // so that we can change the underlying cell value to reflect
            // the mouse click.

            Click += MyGridButtonXEditControlClick;
        }

#region InitializeContext

        /// <summary>
        /// Initializes the color table for the button
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="style"></param>
        public override void
            InitializeContext(GridCell cell, CellVisualStyle style)
        {
            base.InitializeContext(cell, style);
        }

#endregion

#region MyGridButtonXEditControlClick

        /// <summary>
        /// Handles user clicks of the button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MyGridButtonXEditControlClick(object sender, EventArgs e)
        {
            bool running = (EditorCell.Value != null &&
                    EditorCell.Value.Equals("ON"));
            EditorCell.Value = (running == true) ? "OFF" : "ON";
        }

#endregion
    }
    internal class MyGridButtonXEditControl3 : GridButtonXEditControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public MyGridButtonXEditControl3()
        {
            // We want to be notified when the user clicks the button
            // so that we can change the underlying cell value to reflect
            // the mouse click.

            Click += MyGridButtonXEditControlClick;
        }

#region InitializeContext

        /// <summary>
        /// Initializes the color table for the button
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="style"></param>
        public override void
            InitializeContext(GridCell cell, CellVisualStyle style)
        {
            base.InitializeContext(cell, style);

            bool running = Text.Equals("运动至") == false;

            ColorTable = (running == true)
                    ? eButtonColor.OrangeWithBackground
                    : eButtonColor.BlueOrb;
        }

#endregion

#region MyGridButtonXEditControlClick

        /// <summary>
        /// Handles user clicks of the button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MyGridButtonXEditControlClick(object sender, EventArgs e)
        {
            bool running = (EditorCell.Value != null &&
                    EditorCell.Value.Equals("运动至"));
            if(running)
                EditorCell.Value = "运动中...";
        }

#endregion
    }
}
